package com.aybived;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.drawables.Animation;
import com.drawables.LevelTile;
import com.drawables.Obstacle;

import android.app.Activity;
import android.content.Context;
import android.content.res.XmlResourceParser;

public class Loader extends Activity {
//	public static LinkedList<LevelTile> loadXML(XmlResourceParser parser) {
//		
//		LinkedList<LevelTile> tiles = new LinkedList<LevelTile>();
//				
//		try {
//			while (parser.getEventType() != XmlResourceParser.END_DOCUMENT) {
//			     if (parser.getEventType() == XmlResourceParser.START_TAG) { 
//			    	 String tagname = parser.getName();
//			    	 Log.d("XML",tagname);
//			    	 if(tagname.equals("LevelTiles")) {
//			    		 AybivedConfig.OBSTACLE_COUNT = Integer.parseInt(parser.getAttributeValue(0));
//			    	 }
//			    	 if(tagname.equals("Tile")) {
//			    		 Log.d("XML","Creating a Tile");
//			    		 // Parse a Tile
//			    		 LevelTile tile = new LevelTile(parser.getAttributeValue(0));
//			    		 tagname = parser.getName();
//			    		 while(true) {
//			    			 tagname = parser.getName();
//			    			 if(tagname.equals("Object") && parser.getEventType() == XmlResourceParser.START_TAG) {
//			    				 Log.d("XML","Creating an Obstacle");
//			    				 //TODO random object
//			    				 int x = Integer.parseInt(parser.getAttributeValue(0));
//			    				 int y = Integer.parseInt(parser.getAttributeValue(1));
//			    				 Obstacle obstacle = new Obstacle(x, y);
//			    				 tile.addObstacle(obstacle);
//			    			 }
//			    			 if(tagname.equals("Objects") && parser.getEventType() == XmlResourceParser.END_TAG)
//			    				 break;
//			    			 parser.next();
//			    		 }
//			    		 tiles.add(tile);
//			    		 
//			    	 }
//			     }
//			     parser.next();
//			}
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//			return null;
//		} 
//		
//		// Load Obstacles
//		main.obstacleGraphics = new LinkedList<ObstacleGraphics>();
//		
//		return tiles;
//	}
	
	private Context context;

	public Loader(Context context)
	{
		this.context = context;
	}
/*	
	public ArrayList<Obstacle> getObstacleImageList() {
		XmlResourceParser parser = context.getResources().getXml(R.xml.levels);
		
		ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
		try {
			while (parser.getEventType() != XmlResourceParser.END_DOCUMENT) {
			     if (parser.getEventType() == XmlResourceParser.START_TAG) { 
			    	 String tagname = parser.getName();
			    	 
			    	 // Load Obstacle Bitmap
			    	 if(tagname.equals("ObstacleImage")) {
			    		 bitmaps.add(BitmapFactory.decodeResource(main.resource, main.resource.getIdentifier("com.aybived:drawable/"+parser.getAttributeValue(0), null, null)));
			    	 }
			     }
			     parser.next();
			}
			
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bitmaps;
	}
*/
	
	public ArrayList<LevelTile> getLevelTilesList() {
		XmlResourceParser parser = context.getResources().getXml(R.xml.levels);
		
		ArrayList<LevelTile> levelTiles = new ArrayList<LevelTile>();
		try {
			String tileFileName = "";
			String tagname;
			String levelStartTag = "";
			String levelEndTag = "";
			ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
			while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
				
				tagname = parser.getName();
				
				// ON START TAG
				if(parser.getEventType() == XmlPullParser.START_TAG) {
					// get leveltile filename
					if(tagname.equals("LevelTile")) {
						tileFileName = parser.getAttributeValue(0);
						levelStartTag = parser.getAttributeValue(1);
						levelEndTag = parser.getAttributeValue(2);
					}
					// get obstacle coordinates
					/*if(tagname.equals("Object")) {
						obstacles.add(new Obstacle(
								Integer.parseInt(parser.getAttributeValue(0)), 
								Integer.parseInt(parser.getAttributeValue(1)) 
								));
					}*/
				}
				
				// ON END TAG
			    if (parser.getEventType() == XmlPullParser.END_TAG) { 
			    	
			    	//initialize Level Tile and clear obstacles list
			    	if(tagname.equals("LevelTile")) {
	    				levelTiles.add(new LevelTile(tileFileName,obstacles,levelStartTag,levelEndTag));
	    				obstacles.clear();
			    	}
			     }
			    parser.next();
			}
			
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return levelTiles;
	}
	
	public ArrayList<Animation> getAnimations() {
		XmlResourceParser parser = context.getResources().getXml(R.xml.levels);
		
		ArrayList<Animation> animations = new ArrayList<Animation>();
		try {
			String name = "";
			int frames = 0;
			int posX = 0, posY = 0;
			String tagname;
			while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
				
				tagname = parser.getName();
				
				// ON START TAG
				if(parser.getEventType() == XmlPullParser.START_TAG) {
					// get animation name
					if(tagname.equals("Animation")) {
						name = parser.getAttributeValue(0);
						frames = Integer.parseInt(parser.getAttributeValue(1));
						posX = Integer.parseInt(parser.getAttributeValue(2));
						posY = Integer.parseInt(parser.getAttributeValue(3));
					}
				}
				
				// ON END TAG
			    if (parser.getEventType() == XmlPullParser.END_TAG) { 
			    	
			    	//initialize Level Tile and clear obstacles list
			    	if(tagname.equals("Animation")) {
			    		animations.add(new Animation( name, frames, posX, posY));
			    	}
			     }
			    parser.next();
			}
			
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return animations;
	}

}
